package com.hclc.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hclc.api.job.IJobAndTriggerService;
import com.hclc.config.context.AppContext;
import com.hclc.entity.dto.JobAndTriggerDto;
import com.hclc.entity.model.enums.QuartzEnums;
import com.hclc.mapper.customize.JobAndTriggerMapper;
import com.hclc.service.QuartzTemplateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * @创建人 ws
 * @郵箱 zhisirudan@gmail.com
 * @创建时间 2019/11/9
 */
@Service
@Slf4j
public class QuartzTemplateServiceImpl implements QuartzTemplateService {
    @Autowired
    private IJobAndTriggerService jobAndTriggerService;

    @Autowired
    private JobAndTriggerMapper jobAndTriggerMapper;

    /**
     * 查询所有定时器
     *
     * @param iPage
     * @param quartzEnums
     * @author ws
     * @mail zhisirudan@gmail.com
     * @date 上午10:22
     */
    @Override
    public IPage<JobAndTriggerDto> getList(Page<JobAndTriggerDto> iPage, QuartzEnums quartzEnums) {
        IPage<JobAndTriggerDto> jobAndTriggerDetailsWs = jobAndTriggerMapper.getJobAndTriggerDetailsWs(iPage, quartzEnums);
        return jobAndTriggerDetailsWs;
    }

    /**
     * 添加定时任务
     *
     * @param quartzEnums
     * @param cron
     * @param map
     * @author ws
     * @mail zhisirudan@gmail.com
     * @date 上午10:22
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addQuartz(QuartzEnums quartzEnums, String cron, Map map) {
        String currentUserId = AppContext.getCurrentUserId();
        LocalDateTime now = LocalDateTime.now();
        String jobClassName = quartzEnums.getPackageClass();
        String jobGroupName = quartzEnums.name() + "_" + currentUserId + "_" + now.toString();
        String cronExpression = cron;
        String jobDescription = quartzEnums.getDesc();
        try {
            jobAndTriggerService.addJob(jobClassName, jobGroupName, cronExpression, jobDescription, map);
        } catch (Exception e) {
            log.error("添加定时任务失败,quartzEnums:{},cron:{},map:{}", quartzEnums, cron, map);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
        JobAndTriggerDto jobByGroup = jobAndTriggerMapper.getJobByGroup(jobGroupName);
        jobByGroup.setType(quartzEnums.getCode());
        return true;
    }


    /**
     * 暂停定时任务
     *
     * @param jobClassName
     * @param jobGroupName
     * @author ws
     * @mail zhisirudan@gmail.com
     * @date 上午10:22
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean pauseQuartz(String jobClassName, String jobGroupName) {
        try {
            jobAndTriggerService.pauseJob(jobClassName, jobGroupName);
        } catch (Exception e) {
            log.error("暂停定时任务失败,jobClassName:{},jobGroupName:{}", jobClassName, jobGroupName);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
        return true;
    }

    /**
     * 恢复定时任务
     *
     * @param jobClassName
     * @param jobGroupName
     * @author ws
     * @mail zhisirudan@gmail.com
     * @date 上午10:22
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean resumeQuartz(String jobClassName, String jobGroupName) {
        try {
            jobAndTriggerService.resumeJob(jobClassName, jobGroupName);
        } catch (Exception e) {
            log.error("恢复定时任务失败,jobClassName:{},jobGroupName:{}", jobClassName, jobGroupName);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
        return true;
    }

    /**
     * 重新设置任务
     *
     * @param jobClassName
     * @param jobGroupName
     * @param cron
     * @author ws
     * @mail zhisirudan@gmail.com
     * @date 上午10:22
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean rescheduleQuartz(String jobClassName, String jobGroupName, String cron) {
        try {
            jobAndTriggerService.updateJob(jobClassName, jobGroupName, cron);
        } catch (Exception e) {
            log.error("更新定时任务失败,jobClassName:{},jobGroupName:{}，cron:{}", jobClassName, jobGroupName, cron);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
        return true;
    }

    /**
     * 删除定时任务
     *
     * @param jobClassName
     * @param jobGroupName
     * @author ws
     * @mail zhisirudan@gmail.com
     * @date 上午10:22
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean delQuartz(String jobClassName, String jobGroupName) {
        try {
            jobAndTriggerService.deleteJob(jobClassName, jobGroupName);
        } catch (Exception e) {
            log.error("删除定时任务失败,jobClassName:{},jobGroupName:{}", jobClassName, jobGroupName);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
        return true;
    }
}
